package com.ruoyi.business.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.ruoyi.business.domain.ProjectDetail;
import com.ruoyi.business.domain.vo.ProjectDetailVO;
import com.ruoyi.business.mapper.ProjectDetailMapper;
import com.ruoyi.business.service.ProjectDetailService;
import org.springframework.stereotype.Service;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.utils.StringUtils;

import java.util.Map;

import static com.ruoyi.business.domain.table.BankCardTableDef.BANK_CARD;
import static com.ruoyi.business.domain.table.ProjectDetailTableDef.PROJECT_DETAIL;
import static com.ruoyi.business.domain.table.ProjectDetailTemplateTableDef.PROJECT_DETAIL_TEMPLATE;
import static com.ruoyi.business.domain.table.ProjectTableDef.PROJECT;

/**
 * 服务实现 : 工单明细
 *
 * @Author : ljq
 * @Created : 2025/10/12
 */
@Service
public class ProjectDetailServiceImpl extends ServiceImpl<ProjectDetailMapper, ProjectDetail> implements ProjectDetailService {

    @Override
    public Page<ProjectDetailVO> listByCondition(ProjectDetail entity, PageDomain domain) {
        QueryChain<ProjectDetail> queryChain = QueryChain.of(this.getMapper())
                .select(PROJECT_DETAIL.DEFAULT_COLUMNS)
                .from(PROJECT_DETAIL).as("t1")
                .leftJoin(PROJECT).as("t2").on(PROJECT_DETAIL.PROJECT_ID.eq(PROJECT.ID))
                .leftJoin(BANK_CARD).as("t3").on(PROJECT_DETAIL.BANK_CARD_ID.eq(BANK_CARD.ID))
                ;

        this.baseCondition(queryChain, entity);
        {
            Map<String, Object> params = entity.getParams();
            String projectNameLike = params.getOrDefault("projectNameLike", "").toString();
            if (StringUtils.isNotEmpty(projectNameLike)) {
                queryChain.where(PROJECT.NAME.like(projectNameLike));
            }
        }

        return queryChain
                .withRelations()
                .pageAs(new Page<>(domain.getPageNum(), domain.getPageSize()), ProjectDetailVO.class);
    }

    /* ------------------- 基础查询条件 --------------------- */
    private QueryChain baseCondition(QueryChain chain, ProjectDetail entity) {
        // 项目关联ID
        if (StringUtils.isNotEmpty(entity.getProjectId())) {
            chain.where(PROJECT_DETAIL.PROJECT_ID.eq(entity.getProjectId()));
        }
        // 用户关联ID
        if (StringUtils.isNotEmpty(entity.getUserId())) {
            chain.where(PROJECT_DETAIL.USER_ID.eq(entity.getUserId()));
        }
        // 银行卡关联ID
        if (StringUtils.isNotEmpty(entity.getBankCardId())) {
            chain.where(PROJECT_DETAIL.BANK_CARD_ID.eq(entity.getBankCardId()));
        }
        // 计划上班时间区间
        if (StringUtils.isNotEmpty(entity.getPlanWorkTimeRange())) {
            chain.where(PROJECT_DETAIL.PLAN_WORK_TIME_RANGE.eq(entity.getPlanWorkTimeRange()));
        }
        // 工作日期
        if (StringUtils.isNotEmpty(entity.getWorkDate())) {
            chain.where(PROJECT_DETAIL.WORK_DATE.eq(entity.getWorkDate()));
        }
        // 备注
        if (StringUtils.isNotEmpty(entity.getComment())) {
            chain.where(PROJECT_DETAIL.COMMENT.eq(entity.getComment()));
        }
        // 类型
        if (StringUtils.isNotEmpty(entity.getType())) {
            chain.where(PROJECT_DETAIL.TYPE.eq(entity.getType()));
        }
        // 状态
        if (StringUtils.isNotEmpty(entity.getStatus())) {
            chain.where(PROJECT_DETAIL.STATUS.eq(entity.getStatus()));
        }
        // 审批人
        if (StringUtils.isNotEmpty(entity.getApprovalBy())) {
            chain.where(PROJECT_DETAIL.APPROVAL_BY.eq(entity.getApprovalBy()));
        }
        // 审批人驳回原因
        if (StringUtils.isNotEmpty(entity.getApprovalComment())) {
            chain.where(PROJECT_DETAIL.APPROVAL_COMMENT.eq(entity.getApprovalComment()));
        }
        // 工种
        if (StringUtils.isNotEmpty(entity.getWorkType())) {
            chain.where(PROJECT_DETAIL.WORK_TYPE.eq(entity.getWorkType()));
        }
        // 创建者
        if (StringUtils.isNotEmpty(entity.getCreateBy())) {
            chain.where(PROJECT_DETAIL.CREATE_BY.eq(entity.getCreateBy()));
        }
        // 更新者
        if (StringUtils.isNotEmpty(entity.getUpdateBy())) {
            chain.where(PROJECT_DETAIL.UPDATE_BY.eq(entity.getUpdateBy()));
        }
        // 删除标志
        if (StringUtils.isNotNull(entity.getDelFlag())) {
            chain.where(PROJECT_DETAIL.DEL_FLAG.eq(entity.getDelFlag()));
        }

        Map<String, Object> params = entity.getParams();


        // 实际上班时间-模糊匹配
        String realWorkStartLike = params.getOrDefault("realWorkStartLike", "").toString();
        if (StringUtils.isNotEmpty(realWorkStartLike)) {
            String[] split = realWorkStartLike.split(",");
            chain.where(PROJECT_DETAIL_TEMPLATE.REAL_WORK_START_AT.like(realWorkStartLike));
        }

        // 实际下班时间-模糊匹配
        String realWorkEndAtLike = params.getOrDefault("realWorkEndAtLike", "").toString();
        if (StringUtils.isNotEmpty(realWorkEndAtLike)) {
            String[] split = realWorkEndAtLike.split(",");
            chain.where(PROJECT_DETAIL_TEMPLATE.REAL_WORK_END_AT.like(realWorkEndAtLike));
        }


        // 更新时间
        String updateTimeRange = params.getOrDefault("updateTimeRange", "").toString();
        if (StringUtils.isNotEmpty(updateTimeRange)) {
            String[] split = updateTimeRange.split(",");
            chain.where(PROJECT_DETAIL.UPDATE_TIME.between(split[0], split[1]));
        }

        // 项目关联ID-模糊匹配
        String projectIdLike = params.getOrDefault("projectIdLike", "").toString();
        if (StringUtils.isNotEmpty(projectIdLike)) {
            chain.where(PROJECT_DETAIL.PROJECT_ID.like(projectIdLike));
        }

        // 用户关联ID-模糊匹配
        String userIdLike = params.getOrDefault("userIdLike", "").toString();
        if (StringUtils.isNotEmpty(userIdLike)) {
            chain.where(PROJECT_DETAIL.USER_ID.like(userIdLike));
        }

        // 银行卡关联ID-模糊匹配
        String bankCardIdLike = params.getOrDefault("bankCardIdLike", "").toString();
        if (StringUtils.isNotEmpty(bankCardIdLike)) {
            chain.where(PROJECT_DETAIL.BANK_CARD_ID.like(bankCardIdLike));
        }

        // 计划上班时间区间-模糊匹配
        String planWorkTimeRangeLike = params.getOrDefault("planWorkTimeRangeLike", "").toString();
        if (StringUtils.isNotEmpty(planWorkTimeRangeLike)) {
            chain.where(PROJECT_DETAIL.PLAN_WORK_TIME_RANGE.like(planWorkTimeRangeLike));
        }

        // 工作日期-模糊匹配
        String workDateLike = params.getOrDefault("workDateLike", "").toString();
        if (StringUtils.isNotEmpty(workDateLike)) {
            chain.where(PROJECT_DETAIL.WORK_DATE.like(workDateLike));
        }

        // 备注-模糊匹配
        String commentLike = params.getOrDefault("commentLike", "").toString();
        if (StringUtils.isNotEmpty(commentLike)) {
            chain.where(PROJECT_DETAIL.COMMENT.like(commentLike));
        }

        // 类型-模糊匹配
        String typeLike = params.getOrDefault("typeLike", "").toString();
        if (StringUtils.isNotEmpty(typeLike)) {
            chain.where(PROJECT_DETAIL.TYPE.like(typeLike));
        }

        // 状态-模糊匹配
        String statusLike = params.getOrDefault("statusLike", "").toString();
        if (StringUtils.isNotEmpty(statusLike)) {
            chain.where(PROJECT_DETAIL.STATUS.like(statusLike));
        }

        // 审批人-模糊匹配
        String approvalByLike = params.getOrDefault("approvalByLike", "").toString();
        if (StringUtils.isNotEmpty(approvalByLike)) {
            chain.where(PROJECT_DETAIL.APPROVAL_BY.like(approvalByLike));
        }

        // 审批人驳回原因-模糊匹配
        String approvalCommentLike = params.getOrDefault("approvalCommentLike", "").toString();
        if (StringUtils.isNotEmpty(approvalCommentLike)) {
            chain.where(PROJECT_DETAIL.APPROVAL_COMMENT.like(approvalCommentLike));
        }

        // 工种-模糊匹配
        String workTypeLike = params.getOrDefault("workTypeLike", "").toString();
        if (StringUtils.isNotEmpty(workTypeLike)) {
            chain.where(PROJECT_DETAIL.WORK_TYPE.like(workTypeLike));
        }

        // 创建者-模糊匹配
        String createByLike = params.getOrDefault("createByLike", "").toString();
        if (StringUtils.isNotEmpty(createByLike)) {
            chain.where(PROJECT_DETAIL.CREATE_BY.like(createByLike));
        }

        // 更新者-模糊匹配
        String updateByLike = params.getOrDefault("updateByLike", "").toString();
        if (StringUtils.isNotEmpty(updateByLike)) {
            chain.where(PROJECT_DETAIL.UPDATE_BY.like(updateByLike));
        }

        // 用户关联ID-下拉框多选
        String userIdList = params.getOrDefault("userIdList", "").toString();
        if (StringUtils.isNotEmpty(userIdList)) {
            chain.where(PROJECT_DETAIL.USER_ID.in(userIdList.split(",")));
        }

        // 银行卡关联ID-下拉框多选
        String bankCardIdList = params.getOrDefault("bankCardIdList", "").toString();
        if (StringUtils.isNotEmpty(bankCardIdList)) {
            chain.where(PROJECT_DETAIL.BANK_CARD_ID.in(bankCardIdList.split(",")));
        }

        // 类型-下拉框多选
        String typeList = params.getOrDefault("typeList", "").toString();
        if (StringUtils.isNotEmpty(typeList)) {
            chain.where(PROJECT_DETAIL.TYPE.in(typeList.split(",")));
        }

        // 状态-下拉框多选
        String statusList = params.getOrDefault("statusList", "").toString();
        if (StringUtils.isNotEmpty(statusList)) {
            chain.where(PROJECT_DETAIL.STATUS.in(statusList.split(",")));
        }

        // 审批人-下拉框多选
        String approvalByList = params.getOrDefault("approvalByList", "").toString();
        if (StringUtils.isNotEmpty(approvalByList)) {
            chain.where(PROJECT_DETAIL.APPROVAL_BY.in(approvalByList.split(",")));
        }

        // ------------------------------ 排序 ----------------------------------
        String orderBy = params.getOrDefault("orderBy", "").toString();
        if (StringUtils.isEmpty(orderBy)) {
            chain.orderBy(PROJECT_DETAIL.CREATE_TIME.desc());
        } else {
            // TODO 排序
        }


        return chain;
    }
}



